﻿using System.Drawing;
using System.Windows.Forms;
using Romulus;
using SolKeyTest.Data;

namespace SolKeyTest
{
    public partial class Locksmith : Form
    {
        SolScreenControl solScreen;
        OpenFileDialog ofd;
        private ToolStrip toolStrip1;
        private ToolStripButton openToolStripButton;
        private ToolStripButton saveToolStripButton;
        private ToolStripSeparator toolStripSeparator9;
        private ToolStripButton toolStripEnemiesBtn;
        private ToolStripButton toolStripItemsBtn;
        private ToolStripButton toolStripDanaStartBtn;
        private ToolStripButton toolStripDoorwayBtn;
        private ToolStripButton toolStripKeyPosBtn;
        private ToolStripSeparator toolStripSeparator8;
        private ToolStripButton prevLevelBtn;
        private ToolStripButton nextLevelBtn;
        private MenuStrip menuStrip1;
        private ToolStripMenuItem fileToolStripMenuItem1;
        private ToolStripMenuItem openROMToolStripMenuItem1;
        private ToolStripSeparator toolStripSeparator10;
        private ToolStripMenuItem saveROMToolStripMenuItem1;
        private ToolStripMenuItem saveROMAsToolStripMenuItem1;
        private ToolStripSeparator toolStripSeparator11;
        private ToolStripMenuItem exitToolStripMenuItem1;
        private ToolStripMenuItem helpToolStripMenuItem;
        private ToolStripMenuItem aboutToolStripMenuItem2;
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel currentLevelLbl;
        private ToolStripStatusLabel toolStripStatusLabel1;
        SolRom rom;

        public Locksmith() {
            InitializeComponent();

            // Get filename
            if (ofd.ShowDialog() != DialogResult.OK) {
                Close();
            } else {
                // Open rom
                rom = new SolRom(ofd.FileName);
                
                // Show tiles
                solScreen.Rom = rom;
                solScreen.RenderScreen();

                // Create undo-able action queue
                actions = new UndoRedo(rom);
            }
        }

        // Create controls
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Locksmith));
            this.ofd = new System.Windows.Forms.OpenFileDialog();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.openToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.saveToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator9 = new System.Windows.Forms.ToolStripSeparator();
            this.toolStripEnemiesBtn = new System.Windows.Forms.ToolStripButton();
            this.toolStripItemsBtn = new System.Windows.Forms.ToolStripButton();
            this.toolStripDanaStartBtn = new System.Windows.Forms.ToolStripButton();
            this.toolStripDoorwayBtn = new System.Windows.Forms.ToolStripButton();
            this.toolStripKeyPosBtn = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator8 = new System.Windows.Forms.ToolStripSeparator();
            this.prevLevelBtn = new System.Windows.Forms.ToolStripButton();
            this.nextLevelBtn = new System.Windows.Forms.ToolStripButton();
            this.solScreen = new SolKeyTest.SolScreenControl();
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.openROMToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator10 = new System.Windows.Forms.ToolStripSeparator();
            this.saveROMToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.saveROMAsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator11 = new System.Windows.Forms.ToolStripSeparator();
            this.exitToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.aboutToolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.currentLevelLbl = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.toolStrip1.SuspendLayout();
            this.menuStrip1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // ofd
            // 
            this.ofd.Filter = "NES ROM|*.nes";
            this.ofd.Title = "Open Solomon\'s Key Rom";
            // 
            // toolStrip1
            // 
            this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.openToolStripButton,
            this.saveToolStripButton,
            this.toolStripSeparator9,
            this.toolStripEnemiesBtn,
            this.toolStripItemsBtn,
            this.toolStripDanaStartBtn,
            this.toolStripDoorwayBtn,
            this.toolStripKeyPosBtn,
            this.toolStripSeparator8,
            this.prevLevelBtn,
            this.nextLevelBtn});
            this.toolStrip1.Location = new System.Drawing.Point(0, 24);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(257, 25);
            this.toolStrip1.TabIndex = 2;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // openToolStripButton
            // 
            this.openToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.openToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("openToolStripButton.Image")));
            this.openToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.openToolStripButton.Name = "openToolStripButton";
            this.openToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.openToolStripButton.Text = "&Open";
            // 
            // saveToolStripButton
            // 
            this.saveToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.saveToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("saveToolStripButton.Image")));
            this.saveToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.saveToolStripButton.Name = "saveToolStripButton";
            this.saveToolStripButton.Size = new System.Drawing.Size(23, 22);
            this.saveToolStripButton.Text = "&Save";
            // 
            // toolStripSeparator9
            // 
            this.toolStripSeparator9.Name = "toolStripSeparator9";
            this.toolStripSeparator9.Size = new System.Drawing.Size(6, 25);
            // 
            // toolStripEnemiesBtn
            // 
            this.toolStripEnemiesBtn.CheckOnClick = true;
            this.toolStripEnemiesBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripEnemiesBtn.Image = global::SolKeyTest.Properties.Resources.spawn;
            this.toolStripEnemiesBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripEnemiesBtn.Name = "toolStripEnemiesBtn";
            this.toolStripEnemiesBtn.Size = new System.Drawing.Size(23, 22);
            this.toolStripEnemiesBtn.Text = "toolStripEnemiesButton";
            this.toolStripEnemiesBtn.ToolTipText = "Show Enemies List";
            // 
            // toolStripItemsBtn
            // 
            this.toolStripItemsBtn.CheckOnClick = true;
            this.toolStripItemsBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripItemsBtn.Image = global::SolKeyTest.Properties.Resources.bell;
            this.toolStripItemsBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripItemsBtn.Name = "toolStripItemsBtn";
            this.toolStripItemsBtn.Size = new System.Drawing.Size(23, 22);
            this.toolStripItemsBtn.Text = "toolStripButton2";
            this.toolStripItemsBtn.ToolTipText = "Show Items List";
            // 
            // toolStripDanaStartBtn
            // 
            this.toolStripDanaStartBtn.CheckOnClick = true;
            this.toolStripDanaStartBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripDanaStartBtn.Image = global::SolKeyTest.Properties.Resources.dana;
            this.toolStripDanaStartBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripDanaStartBtn.Name = "toolStripDanaStartBtn";
            this.toolStripDanaStartBtn.Size = new System.Drawing.Size(23, 22);
            this.toolStripDanaStartBtn.Text = "toolStripButton1";
            this.toolStripDanaStartBtn.ToolTipText = "Set Player Start Position";
            // 
            // toolStripDoorwayBtn
            // 
            this.toolStripDoorwayBtn.CheckOnClick = true;
            this.toolStripDoorwayBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripDoorwayBtn.Image = global::SolKeyTest.Properties.Resources.doorway;
            this.toolStripDoorwayBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripDoorwayBtn.Name = "toolStripDoorwayBtn";
            this.toolStripDoorwayBtn.Size = new System.Drawing.Size(23, 22);
            this.toolStripDoorwayBtn.Text = "toolStripButton1";
            this.toolStripDoorwayBtn.ToolTipText = "Position of exit";
            // 
            // toolStripKeyPosBtn
            // 
            this.toolStripKeyPosBtn.CheckOnClick = true;
            this.toolStripKeyPosBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.toolStripKeyPosBtn.Image = global::SolKeyTest.Properties.Resources.key;
            this.toolStripKeyPosBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripKeyPosBtn.Name = "toolStripKeyPosBtn";
            this.toolStripKeyPosBtn.Size = new System.Drawing.Size(23, 22);
            this.toolStripKeyPosBtn.Text = "toolStripButton1";
            // 
            // toolStripSeparator8
            // 
            this.toolStripSeparator8.Name = "toolStripSeparator8";
            this.toolStripSeparator8.Size = new System.Drawing.Size(6, 25);
            // 
            // prevLevelBtn
            // 
            this.prevLevelBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.prevLevelBtn.Enabled = false;
            this.prevLevelBtn.Image = global::SolKeyTest.Properties.Resources.arrowback;
            this.prevLevelBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.prevLevelBtn.Name = "prevLevelBtn";
            this.prevLevelBtn.Size = new System.Drawing.Size(23, 22);
            this.prevLevelBtn.Text = "toolStripButton1";
            // 
            // nextLevelBtn
            // 
            this.nextLevelBtn.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.nextLevelBtn.Enabled = false;
            this.nextLevelBtn.Image = global::SolKeyTest.Properties.Resources.arrownext;
            this.nextLevelBtn.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.nextLevelBtn.Name = "nextLevelBtn";
            this.nextLevelBtn.Size = new System.Drawing.Size(23, 22);
            this.nextLevelBtn.Text = "toolStripButton2";
            // 
            // solScreen
            // 
            this.solScreen.LevelNum = 1;
            this.solScreen.Location = new System.Drawing.Point(0, 52);
            this.solScreen.Name = "solScreen";
            this.solScreen.Patterns = null;
            this.solScreen.Rom = null;
            this.solScreen.Size = new System.Drawing.Size(256, 256);
            this.solScreen.SpritePatterns = null;
            this.solScreen.TabIndex = 0;
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem1,
            this.helpToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(257, 24);
            this.menuStrip1.TabIndex = 3;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem1
            // 
            this.fileToolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.openROMToolStripMenuItem1,
            this.toolStripSeparator10,
            this.saveROMToolStripMenuItem1,
            this.saveROMAsToolStripMenuItem1,
            this.toolStripSeparator11,
            this.exitToolStripMenuItem1});
            this.fileToolStripMenuItem1.Name = "fileToolStripMenuItem1";
            this.fileToolStripMenuItem1.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem1.Text = "&File";
            // 
            // openROMToolStripMenuItem1
            // 
            this.openROMToolStripMenuItem1.Name = "openROMToolStripMenuItem1";
            this.openROMToolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
            this.openROMToolStripMenuItem1.Size = new System.Drawing.Size(185, 22);
            this.openROMToolStripMenuItem1.Text = "&Open ROM...";
            // 
            // toolStripSeparator10
            // 
            this.toolStripSeparator10.Name = "toolStripSeparator10";
            this.toolStripSeparator10.Size = new System.Drawing.Size(182, 6);
            // 
            // saveROMToolStripMenuItem1
            // 
            this.saveROMToolStripMenuItem1.Name = "saveROMToolStripMenuItem1";
            this.saveROMToolStripMenuItem1.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
            this.saveROMToolStripMenuItem1.Size = new System.Drawing.Size(185, 22);
            this.saveROMToolStripMenuItem1.Text = "&Save ROM...";
            // 
            // saveROMAsToolStripMenuItem1
            // 
            this.saveROMAsToolStripMenuItem1.Name = "saveROMAsToolStripMenuItem1";
            this.saveROMAsToolStripMenuItem1.ShortcutKeys = System.Windows.Forms.Keys.F12;
            this.saveROMAsToolStripMenuItem1.Size = new System.Drawing.Size(185, 22);
            this.saveROMAsToolStripMenuItem1.Text = "&Save ROM As...";
            // 
            // toolStripSeparator11
            // 
            this.toolStripSeparator11.Name = "toolStripSeparator11";
            this.toolStripSeparator11.Size = new System.Drawing.Size(182, 6);
            // 
            // exitToolStripMenuItem1
            // 
            this.exitToolStripMenuItem1.Name = "exitToolStripMenuItem1";
            this.exitToolStripMenuItem1.Size = new System.Drawing.Size(185, 22);
            this.exitToolStripMenuItem1.Text = "E&xit";
            // 
            // helpToolStripMenuItem
            // 
            this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.aboutToolStripMenuItem2});
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
            this.helpToolStripMenuItem.Text = "&Help";
            // 
            // aboutToolStripMenuItem2
            // 
            this.aboutToolStripMenuItem2.Name = "aboutToolStripMenuItem2";
            this.aboutToolStripMenuItem2.Size = new System.Drawing.Size(107, 22);
            this.aboutToolStripMenuItem2.Text = "&About";
            // 
            // statusStrip1
            // 
            this.statusStrip1.BackColor = System.Drawing.SystemColors.Control;
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.currentLevelLbl,
            this.toolStripStatusLabel1});
            this.statusStrip1.Location = new System.Drawing.Point(0, 311);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(257, 22);
            this.statusStrip1.TabIndex = 4;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // currentLevelLbl
            // 
            this.currentLevelLbl.BackColor = System.Drawing.SystemColors.Control;
            this.currentLevelLbl.Name = "currentLevelLbl";
            this.currentLevelLbl.Size = new System.Drawing.Size(77, 17);
            this.currentLevelLbl.Text = "Current Level";
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(0, 17);
            // 
            // Locksmith
            // 
            this.ClientSize = new System.Drawing.Size(257, 333);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.toolStrip1);
            this.Controls.Add(this.menuStrip1);
            this.Controls.Add(this.solScreen);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Locksmith";
            this.Text = "Locksmith";
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        // This function demonstrtates usage of the undoable action queue
        // Click in the blank area of the window to test.
        protected override void OnMouseDown(MouseEventArgs e) {
            base.OnMouseDown(e);
            return;
            //if (e.Button == MouseButtons.Left) {
            //    // Left-click to do an action.
            //    UndoExample();
            //} else {
            //    // Right-click to undo.
            //    // (Bad things happen if you right-click more than you left-click)
            //    actions.Undo();
            //}

        }
        UndoRedo actions;
        void UndoExample() {
            // This is one way to do/undo an action.
            
            // Calling 'Do' performs the specified action and adds it to the undo list
            actions.Do(
                // We're creating a new action on the spot.
                // We pass the constructor two anonymous methods: one for the do action, and one for the undo
                new UndoRedo.SolAction(
                    // Our do action
                    delegate() {
                        Text = "Do"; // Set form text to "Do"
                    },
                    // Our undo action
                    delegate() {
                        Text = "Undo"; // Set form text to "Undo"
                    },
                     "Sample")
                );

            // The above approach is 'compact'. Everything related to the action is in one place.
            // The problem is that things get complicated fast. The Do/Undo code can't just 
            // change ROM data. You also need to update the UI everytime the action is done
            // or undone.

            // An approach that is better, but requires much more work, is to create a class
            // for each type of action. You'd want a class heirarchy of actions... i.e.
            //
            // Action {
            //     GlobalEdit {
            //         ChangePalette
            //         ChangeStartingLives
            //         <etc>
            //     }
            //     LevelEdit {
            //         ScreenEdit {
            //             ChangeBgTile <--
            //             ChangeEnemy
            //         }
            //         SetStartPoint
            //         <etc>
            //         }
            //     }
            //
            // For example, ChangeBgTile inherits ScreenEdit, which inherits LevelEdit, which
            // inherits Action. This way the UI can examine the action and figure out how to update 
            // itself (you probably want to 'dispatch' the actions, so to speak). 
            // 
            // Suppose the user clicks undo, which calls the Undo function on your action queue, which
            // undoes a ChangeBgTile action. The action queue undoes the action, then passes it to
            // the form. The form sees it's a ScreenEdit, and sends it to the screen editing control.  
            // The screen editing control sees that its a ChangeBgTile, and understands that it needs 
            // to redraw the bg. We've dispatched the action and update the UI.
        }

    }


}
